<?php

class InventoryRentForm extends InventoryForm
{
    const FIELD_ID                     = 'id';
    const FIELD_INVENTORY_ID           = 'inventory_id';
    const FIELD_CLIENT_ID              = 'client_id';
    const FIELD_RENT_DATE              = 'rent_date';
    const FIELD_RETURN_DATE            = 'return_date';
    const FIELD_ACTION                 = 'action';

    public function Bind()
    {
        $this->values = array();
        $this->values[self::FIELD_INVENTORY_ID] = Request::GetParam(self::FIELD_INVENTORY_ID);
        $this->values[self::FIELD_CLIENT_ID] = Request::GetParam(self::FIELD_CLIENT_ID);
        $this->values[self::FIELD_RENT_DATE] = Request::GetParam(self::FIELD_RENT_DATE);
        $this->values[self::FIELD_RETURN_DATE] = Request::GetParam(self::FIELD_RETURN_DATE);
        $this->values[self::FIELD_ACTION] = Request::GetParam(self::FIELD_ACTION);
        $this->isValid = $this->Validate();
    }

    public function Save()
    {
        $itemManager = new InventoryRentManager();
        $inventoryRent = new InventoryRent();
        $inventoryRent->SetInventoryId($this->GetValue(self::FIELD_INVENTORY_ID));
        $inventoryRent->SetClientId($this->GetValue(self::FIELD_CLIENT_ID));
        $inventoryRent->SetRentDate($this->GetValue(self::FIELD_RENT_DATE));
        $inventoryRent->SetReturnDate($this->GetValue(self::FIELD_RETURN_DATE));

        $idField = Request::GetParam(self::FIELD_ID);
        if (!empty($idField) && ($this->values[self::FIELD_ACTION] == 'update'))
        {
            $inventoryRent->SetId($idField);
            $itemManager->Update($inventoryRent);
        }
        else
        {
            $itemManager->Insert($inventoryRent);
        }
    }

    protected function Validate()
    {
        $isValid = true;

        foreach ($this->values as $fieldValue)
        {
            if (empty($fieldValue))
            {
                return false;
            }
        }
        return $isValid;
    }

}